
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
magic-promises
Advanced tools
Syntax sugar for dealing with promises in a much simpler way for async-heavy workflows:
npm install magic-promises
Builds the promise chain internally, so your code can be used like you would do with sync strings, arrays, etc:
// With a bit of magic()
const value = await magic(data).map(op1).filter(op2).map(op3);
// NATIVE; the pure-javascript way of dealing with the same is a lot longer
const value = await Promise.all(data.map(op1)).then(files => files.filter(op2)).then(files => Promise.all(files.map(op3)));
// NATIVE; even when we try to make it more readable it is still longer:
let value = await Promise.all(data.map(op1));
value = value.filter(op2);
value = await Promise.all(value.map(op3));
The coolest bit is that there is no API. You can call the methods, properties, etc of the value that you pass to magic():
const value = await magic(3.1).toFixed(1).split('.').map(n => n * 2).join('.');
console.log(value);
// 6.2 (string)
Note: all these must be run in an
async
context. I am using the great librariesmz/fs
andgot
.
This library is specially useful if we want to do things like fetching urls, mapping their arrays, working with strings, etc. For instance, let's read all the files in the current directory:
// You can apply `.map()` straight to the output of magic()
const files = await magic(readdir(__dirname)).map(file => readFile(file, 'utf-8'));
// NATIVE; this is how you'd have to do with vanilla JS
const files = await readdir(__dirname).then(files => files.map(file => readFile(file, 'utf-8')));
// PRO; using my library `fs-array`, based on magic-promises, it gets better:
const files = await dir(__dirname).map(read);
Retrieve a bunch of websites with valid responses
// Retrieve the content of several pages
const urls = ['francisco.io', 'serverjs.io', 'umbrellajs.com'];
const websites = await magic(urls)
.map(url => got(url)) // Fetch the URLs in parallel like Promise.all()
.map(res => res.body) // Retrieve the actual bodies
.filter(Boolean); // Only those bodies with content
// NATIVE; How to do this with traditional Promises + arrays
const urls = ['francisco.io', 'serverjs.io', 'umbrellajs.com'];
const responses = await Promise.all(urls.map(url => got(url))); // Fetch the URLs in parallel
const websites = responses
.map(res => res.body) // Retrieve the actual bodies
.filter(Boolean); // Only those bodies with content
Works with any value that a promise can resolve to:
// Get and parse a CSV file. Promise => text => array => number
const sum = await magic(got('example.com/data.csv'))
.split('\n')
.filter(Boolean)
.map(line => line.split('\t').shift())
.map(num => parseFloat(num, 10))
.reduce((total, num) => total + num, 0);
Libraries based on this:
fs-array
(from me).FAQs
✨ Simplify dealing with promises for async-heavy situations
We found that magic-promises demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.